package com.kehutong.charge.admin;

import com.kehutong.charge.entity.MeterTypeConfig;
import com.kehutong.charge.service.MeterTypeService;
import com.kehutong.common.DeptService;
import com.kehutong.common.DoveClient;
import com.kehutong.common.dto.DeptDTO;
import com.kehutong.common.entity.Root;
import com.kehutong.common.util.Token;
import org.coraframework.authz.HasPermission;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;

import java.util.List;

/**
 * @program: kht
 * @description:  仪表类型
 * @author: joneron
 * @create: 2021/6/21
 **/
@MvcAction
@ReqMapping("/admin/pay/charge/meter/type")
public class MeterTypeController {

    private static final Logger logger = LoggerFactory.getLogger(MeterTypeController.class);

    @Inject
    MeterTypeService meterTypeService;
    @Inject
    DeptService deptService;



    @ReqMapping("/page")
    public Object page(Token token,JSONObject jsonObject) throws Exception{
        if (!Root.isRoot(token.getUuid())) {//不是超级管理员
            return meterTypeService.findPage(jsonObject);
        }
        return Result.success();
    }


    @ReqMapping("/list")
    @HasPermission("pay:charge:meter:type:view")
    public Object list(JSONObject jsonObject) {
        return meterTypeService.findList(jsonObject);
    }


    @Bean(newInstance=true, copy=true)
    @HasPermission("pay:charge:meter:type:edit")
    @ReqMapping("/save")
    public Object save(MeterTypeConfig entity, Token token) throws Exception {

        entity.setCreateName(token.getUsername());

        List<String> departList = token.getDepartments();
        if(null != departList && departList.size() > 0){
            logger.error("用户{}数据异常，部门为空！",token.getUsername());

            DeptDTO deptDTO = deptService.getDept(departList.get(0));
            entity.setDepartmentNo(deptDTO.getDepartmentNo());
            entity.setDepartmentNos(deptDTO.getDepartmentNos());
            entity.setDepartmentName(deptDTO.getDepartmentName());
        }

        return meterTypeService.save(entity);
    }

    @Bean(copy=true)
    @HasPermission("pay:charge:meter:type:edit")
    @ReqMapping("/update")
    public Object update(MeterTypeConfig entity, Token token) throws Exception {

        return meterTypeService.update(entity);
    }

    @HasPermission("pay:charge:meter:type:edit")
    @ReqMapping("/delete")
    public Object delete(MeterTypeConfig entity) {

        return meterTypeService.deleteById(entity.getId());
    }

    @ReqMapping("/setStatus")
    @HasPermission("pay:charge:meter:type:edit")
    public Object setStatus(MeterTypeConfig entity) throws Exception {
        entity.setStatus(!entity.isStatus());
        meterTypeService.update(entity);
        return Result.success();
    }

    @ReqMapping("/get")
    public Object get(MeterTypeConfig entity) {

        return Result.success(entity);
    }
}
